export default function ({ $axios, store, redirect, app }) {
  // 请求拦截器
  $axios.interceptors.request.use(
    (config) => {
      // 在发送请求之前做些什么
      const token = store.getters['auth/token'] || localStorage.getItem('token')
      if (token) {
        config.headers.Authorization = `Bearer ${token}`
      }
      
      // 设置请求超时时间
      config.timeout = 30000
      
      return config
    },
    (error) => {
      // 处理请求错误
      return Promise.reject(error)
    }
  )

  // 响应拦截器
  $axios.interceptors.response.use(
    (response) => {
      // 对响应数据做点什么
      return response.data
    },
    (error) => {
      // 处理响应错误
      const { status, data } = error.response || {}
      
      switch (status) {
        case 400:
          app.$message.error(data.message || '请求参数错误')
          break
        case 401:
          app.$message.error('登录已过期，请重新登录')
          localStorage.removeItem('token')
          redirect('/login')
          break
        case 403:
          app.$message.error('没有权限执行此操作')
          break
        case 404:
          app.$message.error('请求的资源不存在')
          break
        case 500:
          app.$message.error(data.message || '服务器内部错误')
          break
        default:
          app.$message.error(data.message || '请求失败')
      }
      
      return Promise.reject(error)
    }
  )

  // 创建自定义axios实例
  app.$api = {
    // 包管理相关API
    package: {
      getPackages: (params) => $axios.get('/packages', { params }),
      getPackageDetail: (name) => $axios.get(`/packages/${name}`),
      syncPackage: (name) => $axios.post(`/packages/sync`, { name }),
      updatePackage: (name, data) => $axios.put(`/packages/${name}`, data),
      deletePackage: (name) => $axios.delete(`/packages/${name}`),
      searchPackages: (query, params) => $axios.get(`/packages/search?q=${query}`, { params })
    },
    
    // 统计分析相关API
    stats: {
      getDownloadStats: (params) => $axios.get('/stats/downloads', { params }),
      getPopularPackages: (params) => $axios.get('/stats/popular', { params }),
      getTrendData: (params) => $axios.get('/stats/trends', { params })
    },
    
    // 标签系统相关API
    tags: {
      getTags: () => $axios.get('/tags'),
      addTag: (data) => $axios.post('/tags', data),
      updateTag: (id, data) => $axios.put(`/tags/${id}`, data),
      deleteTag: (id) => $axios.delete(`/tags/${id}`),
      getPackageTags: (packageName) => $axios.get(`/packages/${packageName}/tags`),
      addPackageTag: (packageName, tagId) => $axios.post(`/packages/${packageName}/tags`, { tagId }),
      removePackageTag: (packageName, tagId) => $axios.delete(`/packages/${packageName}/tags/${tagId}`)
    },
    
    // 讨论区相关API
    discussions: {
      getDiscussions: (packageName, params) => $axios.get(`/packages/${packageName}/discussions`, { params }),
      createDiscussion: (packageName, data) => $axios.post(`/packages/${packageName}/discussions`, data),
      getDiscussionDetail: (packageName, id) => $axios.get(`/packages/${packageName}/discussions/${id}`),
      updateDiscussion: (packageName, id, data) => $axios.put(`/packages/${packageName}/discussions/${id}`, data),
      deleteDiscussion: (packageName, id) => $axios.delete(`/packages/${packageName}/discussions/${id}`),
      createReply: (packageName, discussionId, data) => $axios.post(`/packages/${packageName}/discussions/${discussionId}/replies`, data)
    },
    
    // 数据同步相关API
    sync: {
      startSync: () => $axios.post('/sync/start'),
      stopSync: () => $axios.post('/sync/stop'),
      getSyncStatus: () => $axios.get('/sync/status'),
      getSyncHistory: () => $axios.get('/sync/history')
    },
    
    // 健康检查
    health: {
      check: () => $axios.get('/health')
    }
  }
}